# ==================================
#
#  Code for replicating:
# "Positioning Under Alternative Electoral Systems: Evidence From Japanese Candidate Election Manifestos"
#  Amy Catalinac, NYU
#
# ==================================







# ==================================
# Regression to test for structural break
# (Table 3 of the Online Appendix)

load("party_dispersion_prepostER.Rdata") # called dat2a

# var = dispersion scores for each party (from "dispersion_in_parties.csv")
# er = a dummy coded 1 for post-ER, 0 for pre-ER
# time = coded 1 (1986) ... 8 (2009)
# elec_2009 = a dummy for 2009

dat2a$er <- as.factor(dat2a$er)
dat2a$elec_2009 <- as.factor(dat2a$elec_2009)
dat2a$pty <- as.factor(dat2a$pty)

cl   <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  coeftest(fm, vcovCL) }

# first column, Table 3
fit1 <- lm(var ~ time + er + time*er, data=dat2a)
no.controls <- cl(dat2a, fit1, dat2a$pty)
print(no.controls)
cat("\n R^2=",summary(fit1)$r.squared,"\n")
cat("\n adj R^2=",summary(fit1)$adj.r.squared,"\n")

# second column, Table 3
fit2 <- lm(var ~ time + er + time*er + pty, data=dat2a)
controls <- cl(dat2a, fit2, dat2a$pty)
print(controls)
cat("\n R^2=",summary(fit2)$r.squared,"\n")
cat("\n adj R^2=",summary(fit2)$adj.r.squared,"\n")

rm(list=ls())




# ==================================
# Chow test

load("party_dispersion_prepostER.Rdata") # called dat2a

# order dat2a by time (identify last row pertaining to old electoral system):
dat2a.order <- dat2a[order(dat2a$time), ]
dat2a.order <- cbind(index(dat2a.order),dat2a.order) # 23rd row is the last observation under old electoral system:
require(strucchange)
sctest(var ~ time, type = "Chow", point = 23, data = dat2a.order)

rm(list=ls())





# ==================================
# Predicted values 
# (Figure 3 in paper)

load("party_dispersion_prepostER.Rdata") # called dat2a

dat2a$er <- as.factor(dat2a$er)

fit1f <- lm(var ~ time + er + time*er, data=dat2a)
nd <- data.frame(time=c(1:8), er=as.factor(c(0,0,0,1,1,1,1,1)))
predictions1 <- predict.lm(fit1f, nd, interval="confidence")

par(mfrow = c(1, 1), mar = c(4,4,2,1), tcl = -0.25, mgp = c(1.75, 0.6, 0),
    font.main = 1, cex.main = 2)
years <- c(1986, 1990, 1993, 1996, 2000, 2003, 2005, 2009)
plot(years, predictions1[,1],  pch=19, main="", xlab="Year", ylab="Predicted Values", xaxt="n", ylim=c(0, 0.85))
axis(1, at=c(1986, 1990, 1993, 1996, 2000, 2003, 2005, 2009))
arrows(years[1], predictions1[1,2], years[1], predictions1[1,3], code = 3, lwd=1, angle=90)
arrows(years[2], predictions1[2,2], years[2], predictions1[2,3], code = 3, lwd=1, angle=90)
arrows(years[3], predictions1[3,2], years[3], predictions1[3,3], code = 3, lwd=1, angle=90)
arrows(years[4], predictions1[4,2], years[4], predictions1[4,3], code = 3, lwd=1, angle=90)
arrows(years[5], predictions1[5,2], years[5], predictions1[5,3], code = 3, lwd=1, angle=90)
arrows(years[6], predictions1[6,2], years[6], predictions1[6,3], code = 3, lwd=1, angle=90)
arrows(years[7], predictions1[7,2], years[7], predictions1[7,3], code = 3, lwd=1, angle=90)
arrows(years[8], predictions1[8,2], years[8], predictions1[8,3], code = 3, lwd=1, angle=90)

